package com.cjack.frenchexcel.service;

import com.cjack.frenchexcel.util.DateFormatUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 集成定时任务
 */
@Service
public class ScheduleService {
    private final static Logger LOG = LogManager.getLogger( ScheduleService.class);

    @Autowired
    QontoService qontoService;
    @Autowired
    MoonCardService moonCardService;
    @Autowired
    UserAdminService userAdminService;

    /**  qonto这个银行的接口不再使用
    //定时任务每天晚上23.59分开始执行，查询今天早上0点到今天晚上24.00的qonto流水
    //每天晚上11.55执行
    @Scheduled(cron = "0 0 4 * * ?")
    public void getTransactionsFromQonton() throws Exception {
        LOG.info( "Schedule handle getTransactionsFromQonton,current time :" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        String startTime = DateFormatUtil.format( DateFormatUtil.getLastDayZero( 3), DateFormatUtil.DATE_RULE_6);
        String endTime = DateFormatUtil.format( new Date(), DateFormatUtil.DATE_RULE_6);;
        qontoService.getTransactionsFromQonto( startTime, endTime);
    } **/

    //定时任务每天凌晨1点执行，查询昨天早上0点到今天早上0点 所有员工当天在mooncard的即时交易流水
    //@Scheduled(cron = "0 0 4 * * ?")
    //@Scheduled(cron = "0 * * * * ?")
    public void handleExpenses() throws Exception {
        LOG.info( "Schedule handle handleExpenses,current time :" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        String startTime = DateFormatUtil.format( DateFormatUtil.getLastDayZero( 1), DateFormatUtil.DATE_RULE_6);
        String endTime = DateFormatUtil.format( DateFormatUtil.getLastDayZero( 0), DateFormatUtil.DATE_RULE_6);
        moonCardService.handleExpenses( startTime, endTime);
    }

    //定时任务每天凌晨1点执行，查询昨天早上0点到今天早上0点 所有员工当天在mooncard的交易流水
    //@Scheduled(cron = "0 0 4 * * ?")
    //@Scheduled(cron = "0 * * * * ?")
    public void handleAccountMovements() throws Exception {
        LOG.info( "Schedule handle handleAccountMovements,current time :" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        String startTime = DateFormatUtil.format( DateFormatUtil.getLastDayZero( 10), DateFormatUtil.DATE_RULE_6);
        String endTime = DateFormatUtil.format( DateFormatUtil.getLastDayZero( 0), DateFormatUtil.DATE_RULE_6);
        moonCardService.checkByAccountMovements( startTime, endTime);
    }


    //定时任务每天凌晨3点执行，根据数据库里面昨天早上0点到今天早上0点 计算该员工今天的日额度信息,
    // 每天的mooncard记录明细，由handleAccountMovements这个定时任务在1点的时候同步完成
    //@Scheduled(cron = "0 0 5 * * ?")
    public void handleDayMooncardLimit(){
        LOG.info( "Schedule handle handleDayMooncardLimit,current time :" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        moonCardService.handleDayMooncardLimit();
    }

    //定时任务每月1号凌晨1点执行，统计员工上月的消费/充值明细，生成excel发送到员工的邮箱
    //@Scheduled(cron = "0 0 6 1 * ?")
    public void handleUserConsumerAndRecharge(){
        LOG.info( "Schedule handle handleUserConsumerAndRecharge,current time :" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        userAdminService.handleUserConsumerAndRecharge();
    }
}
